﻿/*
 * Created by SharpDevelop.
 * User: CeCi
 * Date: 02/04/2014
 * Time: 01:58 p.m.
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data.OleDb;
using System.Data;
using System.Windows.Forms;
using Gestión_UTN.Entities;

namespace Gestión_UTN.daimpl
{
	/// <summary>
	/// Description of materiadaimpl.
	/// </summary>
	public class materiadaimpl:Gestión_UTN.da.materiada
	{
		public materiadaimpl()
		{
		}
		
		public bool SaveMateria(string nombre, int car_id)
		{
			bool ingresada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "INSERT INTO mat_materias(nombre, car_id)" +
                    			"VALUES(@nombre, @car_id)";
					
					OleDbCommand insertar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					object[] values = {nombre,car_id};
					
					insertar.Parameters.AddRange(values);
					
					insertar.ExecuteNonQuery();
					
					trans.Commit();
					
					ingresada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				ingresada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return ingresada;
		}
		
		public bool DeleteMateria(string nombre, int car_id)
		{
			bool eliminada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "DELETE FROM mat_materias " +
						"WHERE nombre = @nombre AND car_id = @car_id";
					
					OleDbCommand eliminar = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					trans = conex.BeginTransaction();
					
					eliminar.Parameters.AddWithValue("@nombre",nombre);
					eliminar.Parameters.AddWithValue("@car_id",car_id);
					
					eliminar.ExecuteNonQuery();
					
					trans.Commit();
					
					eliminada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				eliminada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return eliminada;
		}
		
		public bool ModifyMateria(string nombre_anterior, int car_id_anterior, string nombre, int car_id)
		{
			bool modificada = false;
			
			OleDbConnection conex = null;
			OleDbTransaction trans = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				trans = null;
				
				if (null != conex) {
					
					string sql = "UPDATE mat_materias SET nombre = @nombre," +
									" car_id = @car_id " +
									"WHERE nombre = @nombre_anterior AND car_id = @car_id_anterior";
					
					OleDbCommand modificar = new OleDbCommand(sql,conex);
					
					conex.Open();
					trans = conex.BeginTransaction();
					
					object[] values = {nombre,car_id,nombre_anterior,car_id_anterior};
					
					modificar.Parameters.AddRange(values);
					
					modificar.ExecuteNonQuery();
					
					trans.Commit();
					
					modificada = true;
					
					conex.Close();
				}
				
			} catch (Exception) {
				modificada = false;
				trans.Rollback();
				conex.Close();
			}
			
			return modificada;
		}
		
		public System.Data.DataTable FindAllMaterias()
		{
			DataTable tablaMaterias = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				tablaMaterias = new DataTable();
				
				if (null != conex) {
					
					string sql = "SELECT mat_id, mat_nombre, car_nombre" +
									" FROM mat_materias mat, car_carreras car " +
									"WHERE mat.car_id = car.car_id";
					
					 OleDbDataAdapter adaptador = new OleDbDataAdapter(sql, conex);
					 
					 conex.Open();
					 
					 adaptador.Fill(tablaMaterias,"materias");
					
					 conex.Close();
				}
				
			} catch (Exception) {
				
				tablaMaterias = null;
				conex.Close();
			}
			
			return tablaMaterias;
		}
		
		
		public Materia FindMateriaByMatId(int mat_id)
		{
			Materia materia = null;
			
			OleDbConnection conex = null;
			
			try
			{
				conex = util.Conexion.conectarAccess();
				
				if (null != conex) {
					
					string sql = "SELECT mat_id, mat_nombre, car_id" +
									" FROM mat_materias mat " +
									"WHERE mat_id = @mat_id";
					
					OleDbCommand consulta = new OleDbCommand(sql,conex);
					
					conex.Open();
					
					consulta.Parameters.AddWithValue("@mat_id",mat_id);
					
					OleDbDataReader lector = consulta.ExecuteReader();
					
					while (lector.Read()) 
					{
						materia = new Materia(Convert.ToInt16(lector[0].ToString()),
						                      lector[1].ToString(),
						                      Convert.ToInt16(lector[2].ToString()));
						                                      
					}
					
					conex.Close();
				}
				
			} catch (Exception) {
				
				materia = null;
				conex.Close();
			}
			
			return materia;
		}
	}
}
